function memberShoppingCart(){}
memberShoppingCart.prototype = {
        
    removeTicketMember : function(proCodigo, carCodigo, message){
        const element = document.getElementById(proCodigo);
        const itemData = element.getAttribute('data-ticket-item');
        var thisObject = this;
        
        var modal = new Modal({
            title: translate["shopping-cart-js-delete-confirmation"],
            body: '
'+message+'
',
            confirm: function (){
                ajaxLoader.ajaxStart();
                
                $.ajax({
                    url: '/member-shopping-cart/remove-ticket-member-shopping-cart',
                    method: 'post',
                    data: {pro_codigo: proCodigo, car_codigo: carCodigo},
                    dataType: 'json',
                    error: function(json){
                        ajaxLoader.ajaxStop();
                        
                        var modal = new Modal({
                            title: json.title,
                            body : ''+json.message+'
',
                            buttonOK : false,
                            cancelText: 'OK',
                        });
                        modal.show();
                    },
                    success: function(json) {
                        ajaxLoader.ajaxStop();
                        modal.hide();
                        if(!json.status){
                            var modalError = new Modal({
	                            title: json.title,
	                            body : ''+json.message+'
',
	                            buttonOK : false,
	                        });
	                        modalError.show();
	                    }else{
                            gtmActions.removeFromCart([JSON.parse(itemData)]);
	                        $('#'+proCodigo).remove();
	                        $('#cart_total').text(json.total_cart);
	                        thisObject.changeTotalBoxValues(json);
	                        location.reload();
                        }
                    },
                });
                    
                },                                                                                            
        });
        modal.show();
        
    },
    
    applyMember : function(productId, ticketId, eventId, sectorId, packageId, memberId = null, parking = false){
        var thisObject = this;
        if(memberId == null){
            var memberId = $("#ticketMember"+productId).val();
        }
		const guestId = $('#ticketMember' + productId).find(':selected').attr('data-guest-id');
        var parentTicketId = $('#ticketMember' + productId).find(':selected').attr('data-parent-ticket-id');
        ajaxLoader.ajaxStart();
        
        $.ajax({
            url: '/member-shopping-cart/apply-member-to-ticket',
            data: {productId: productId, ticketId: ticketId, eventId: eventId, memberId: memberId, sectorId:sectorId, packageId:packageId, guestId: guestId, parentTicketId: parentTicketId, parking: parking},
            method: 'post',
            dataType: 'json',
            success: function(json){                
                if(json.status){
					ajaxLoader.ajaxStop();
					// Mostrar mensagem antes de reload
					if( json.message && json.title ) {
						let modalParams = {
							title: json.title,
							body: ''+json.message+'
',
						};
						if( typeof json.redirectUrl === 'string' ) {
							modalParams.confirm = function(){ window.location.href = json.redirectUrl };
							modalParams.buttonCancel = true;
							modalParams.cancel = function(){ location.reload();}; 
						}
						if(json.buttons != undefined){
							if(json.buttons.ok != undefined){
								modalParams.buttonOK = true;
								modalParams.okText = json.buttons.ok;
								modalParams.confirm = function(){window.location.href = json.buttons.okUrl};
							}
							if(json.buttons.cancel != undefined){
								modalParams.cancelText = json.buttons.cancel;
							}
						}
						const modal = new Modal(modalParams);
						modal.show();
						thisObject.changeTotalBoxValues(json);
						thisObject.changeTicketvalues(json, productId);
						return;
					}
                    thisObject.changeTotalBoxValues(json);
                    thisObject.changeTicketvalues(json, productId);
                    if(!parking){
                        location.reload();
                    }else{
                        window.location.href = '/shopping-cart';
                    }
                }else{
                    ajaxLoader.ajaxStop();
					$('#ticketMember'+productId).val('');
                    var modal = new Modal({
                      title: json.title,
                      body : ''+json.message+'
',
                      buttonOK : false,
                    });
                    modal.show();                
                }
            }, error: function(json){
                ajaxLoader.ajaxStop();
                window.location.reload();
            }
        });
        
    },
    
    memberNotSelected : function() {
        $(".ticketMember").addClass("feedback");
    },
    
    cleanMemberTicket : function(productId, ticketId, eventId, sectorId){
        var thisObject = this;
        var memberId = '';
        ajaxLoader.ajaxStart();
        $.ajax({
            url: '/member-shopping-cart/apply-member-to-ticket',
            data: {productId: productId, ticketId: ticketId, eventId: eventId, memberId: memberId, sectorId:sectorId},
            method: 'post',
            dataType: 'json',
            success: function(json){
                if(json.status){
                    thisObject.changeTotalBoxValues(json);
                    thisObject.changeTicketvalues(json, productId);
                    location.reload();
                }else{
                    ajaxLoader.ajaxStop();
                    var modal = new Modal({
                      title: json.title,
                      body : ''+json.message+'
',
                      buttonOK : false,
                    });
                    modal.show();
                }
            }
        });
        
    },
    cleanMemberTicketCat : function(productId, ticketId, eventId, sectorId, carCodigo, message = null){
        var thisObject = this;
        var memberId = '';
        var modalOne = new Modal({
            title: translate["shopping-cart-js-delete-confirmation"],
            body: ''+message+'
',
            confirm: function (){
                ajaxLoader.ajaxStart();
                $.ajax({
                    url: '/member-shopping-cart/apply-member-to-ticket',
                    data: {productId: productId, ticketId: ticketId, eventId: eventId, memberId: memberId, sectorId:sectorId}, carCodigo:carCodigo,
                    method: 'post',
                    dataType: 'json',
                    success: function(json){
                        if(json.status){
                            $.ajax({
                                url: '/member-shopping-cart/remove-ticket-member-shopping-cart',
                                method: 'post',
                                data: {pro_codigo: productId, car_codigo: carCodigo},
                                dataType: 'json',
                                error: function(json){
                                    ajaxLoader.ajaxStop();
                                    var modal = new Modal({
                                        title: json.title,
                                        body : ''+json.message+'
',
                                        buttonOK : false,
                                        cancelText: 'OK',
                                    });
                                    modal.show();
                                },
                                success: function(json) {
                                    ajaxLoader.ajaxStop();
                                    modalOne.hide();
                                    if(!json.status){
                                        var modalError = new Modal({
                                            title: json.title,
                                            body : ''+json.message+'
',
                                            buttonOK : false,
                                        });
                                        modalError.show();
                                    }else{
                                        $('#'+productId).remove();
                                        $('#cart_total').text(json.total_cart);
                                        thisObject.changeTotalBoxValues(json);
                                        location.reload();
                                    }
                                },
                            });
                        }else{
                            var modal = new Modal({
                                title: json.title,
                                body : ''+json.message+'
',
                                buttonOK : false,
                            });
                            modal.show();
                        }
                    }
                });
            },
        });
        modalOne.show();
    },
    
    applyMemberDiscount : function(productId, cartId, eventId, callback){
        var thisObject = this;
        var discountId = $("#ticketMemberDiscount"+productId).val();
        
        if(discountId.charAt(0) == 'L'){
             $('#modal').css('display', 'block');
             $('.modal-backdrop').css('display', 'block');
             $('#form-half-ticket').trigger("reset");
            
             $('#modal').attr("data-pro", productId);
             $('#modal').attr("data-car", cartId);
             $('#modal').attr("data-eve", eventId);
             $('#modal').attr("data-dsc", discountId);
			 if(callback) {
				 callback(discountId);
			 } else {
             	$('#modal').modal('show');
			 }
        }else if(discountId.charAt(0) == 'M'){
             $('#modal-multiclubes').css('display', 'block');
             $('.modal-backdrop').css('display', 'block');
             $('#form-multiclubes').trigger("reset");
             
             $('#modal-multiclubes').attr("data-pro", productId);
             $('#modal-multiclubes').attr("data-car", cartId);
             $('#modal-multiclubes').attr("data-eve", eventId);
             $('#modal-multiclubes').attr("data-dsc", discountId);
             
             $('#modal-multiclubes').modal('show');
        }else{
            thisObject.insertDiscount(productId, cartId, discountId, eventId, null, null, null);
        }
    },
    
    insertDiscount : function(proCodigo, carCodigo, dscCodigo, eveCodigo, rg, dob, nome, email){
        var thisObject = this;
        
        ajaxLoader.ajaxStart();
        $.ajax({
            url: '/member-shopping-cart/insert-discount',
            data: {pro_codigo: proCodigo, car_codigo: carCodigo, dsc_codigo: dscCodigo, eve_codigo: eveCodigo, rg: rg, dob: dob, nome: nome, email: email},
            method: 'post',
            dataType: 'json',
            success: function(json){
                if(json.status){
                    thisObject.changeTotalBoxValues(json);
                    thisObject.changeTicketvalues(json, proCodigo);
                    $('#modal').modal('hide');
                    
                    if(json.message != undefined){
                        ajaxLoader.ajaxStop();
                        
                        var modalParams = {
                            title: json.title,
                            body : ''+json.message+'
',
                            buttonOK : false,
                            cancelText: 'OK',
                            dismissButton : false
                        };
                        if(json.buttons != undefined){
                            if(json.buttons.ok != undefined){
                                modalParams.buttonOK = true;
                                modalParams.okText = json.buttons.ok;
                                modalParams.confirm = 'reload';
                            }
                            if(json.buttons.cancel != undefined){
                                modalParams.cancelText = json.buttons.cancel;
                                modalParams.cancel = function(){
                                    $('#ticketMemberDiscount'+proCodigo).parents('tr').find('i.fa.fa-ban').click();
                                };
                            }
                        }
                      var modal = new Modal(modalParams);
                      modal.show();
                    }
                    else{
                        location.reload();
                    }
                }else{
                    
                    ajaxLoader.ajaxStop();
                    
                    var modalParams = {
                      title: json.title,
                      body : ''+json.message+'
',
                      buttonOK : false,
                      cancelText: 'OK',
                      dismissButton : false,
                      cancel: function(){
                        $('#ticketMemberDiscount'+proCodigo).val('T-2');
                      }
                    };
                    
                    if(json.buttons != undefined){
                        if(json.buttons.ok != undefined){
                            modalParams.buttonOK = true;
                            modalParams.okText = json.buttons.ok;
                            modalParams.confirm = function(){window.location.href = json.buttons.okUrl};
                        }
                        if(json.buttons.cancel != undefined){
                            modalParams.cancelText = json.buttons.cancel;
                        }
                    }
                        
                    var modal = new Modal(modalParams);
                    modal.show();
                }
            }
        });
    },
    
    changeTotalBoxValues : function(jsonData) {
        $('.subtotal').html(jsonData.subtotal);
        $('.discount').html(' - '+jsonData.discount);
        $('.order_tax').html(' + ' + jsonData.order_tax);
        $('.fee_tax').html(jsonData.fee_tax);
        $('.total').html(jsonData.total);
    },
    changeTicketvalues : function(jsonData, proCodigo){
        $('#discount_' + proCodigo).html( jsonData.product_discount );
        $('#convenience_fee' + proCodigo).html(jsonData.product_convenience_fee);
        $('#total_' + proCodigo).html(jsonData.product_price);   
    },
    
    halfTicketMember : function(){
        var thisObject = this;
        $('#modal').modal('hide');
        thisObject.insertDiscount($('#modal').data('pro'), $('#modal').data('car'), $('#modal').attr('data-dsc'), $('#modal').data('eve'), $('#user_rg').val(),$('#user_dob').val(),$('#user_nome').val());
    },
    
    multiclubesTypeTicket : function(){
        $('#modal-multiclubes').modal('hide');
        this.insertDiscount($('#modal-multiclubes').attr('data-pro'), $('#modal-multiclubes').data('car'), $('#modal-multiclubes').data('dsc'), $('#modal-multiclubes').data('eve'), $('#title_code').val());
    },
    
    payment : function(){
        window.location.href = '/policy';
    },
    
    orderGenerate : function(carCodigo, totalShoppingCart){
        ajaxLoader.ajaxStart();
        const element = document.getElementById("shopping-cart");
        const value = document.getElementById("total");
        const itemData = element.getAttribute('data-cart');
       
        $.ajax({
            url: '/shopping-cart/order-generate',
            data: {car_codigo: carCodigo, total_shoppingcart: totalShoppingCart},
            method: 'post',
            dataType: 'json',
            error: function(){
                var modal = new Modal({
                   title: 'Erro',
                   body : '' + translate["shopping-cart-js-order-generate-error"] + '
',
                   buttonOK : false,
                   cancelText: 'OK',
               });
               modal.show();           
            },
            success: function(json){
                if(!json.status){
                    ajaxLoader.ajaxStop();
                    
                    if(json.message == undefined){
                        var message = translate["shopping-cart-js-order-generate-error"];
                    }else{
                        var message = json.message;
                    }
                    var modal = new Modal({
                        title: json.title,
                        body : '' + message + '
',
                        buttonOK : false,
                        cancelText: 'Fechar',
                        cancel: function(){
                            window.scrollTo(0, 0);
                            setTimeout(function() {
                                window.location.reload();
                            }, 1000);
                        }
                    });
                    modal.show(); 
                }else{ 
                    gtmActions.beginCheckout(JSON.parse(itemData), value.innerText.replace("R$ ", "").replace(".", "").replace(",", "."));
                    
                    setTimeout(function() {
                        window.location.replace(json.url);
                    }, 500);
                    
                }
            }
        });
    },
    expireCart: function (){
        ajaxLoader.ajaxStart();
        
        setTimeout(function(){
            $.ajax({
                url: '/member-shopping-cart/expire-cart',
                dataType: 'json',
                method: 'post',
                success: function(json){
                    ajaxLoader.ajaxStop();
                    if(json.status){
                        window.location.href = json.url;
                    }else{
                        window.location.reload();
                    }
                }
            });
        }, 3000);
    },
    ticketAddDocument : function(){
        if(!$('#modal-documento-ingresso').find('#ticket-document-check').prop('checked')){
            alert('Você precisa confirmar que os dados são verdadeiros.');
            return;
        }
        ajaxLoader.ajaxStart();
        var ticketId = $('#modal-documento-ingresso').find('#ticket-document-ticket-id').val();
        var name = $('#modal-documento-ingresso').find('#ticket-document-name').val();
        var document = $('#modal-documento-ingresso').find('#ticket-document-document').val();
        var email = $('#modal-documento-ingresso').find('#ticket-document-email').val();
        var phone = $('#modal-documento-ingresso').find('#ticket-document-phone').val();
        var dob = $('#modal-documento-ingresso').find('#ticket-document-dob').val();
        $.ajax({
            url: '/member-shopping-cart/ticket-add-document',
            data: {ticketId: ticketId, name: name, document: document, email: email, phone: phone, dob: dob},
            dataType: 'json',
            method: 'post',
            success: function(json){
                ajaxLoader.ajaxStop();
                if(json.status){
                    $('#ticket-'+ticketId+'-document-name').text(name);
                    $('#ticket-'+ticketId+'-document-name').attr('onclick', 
                    "$('#modal-documento-ingresso').find('#ticket-document-ticket-id').val("+ticketId+");" +
                    "$('#modal-documento-ingresso').find('#ticket-document-name').val('"+name+"');" + 
                    "$('#modal-documento-ingresso').find('#ticket-document-document').val('"+document+"');" + 
                    "$('#modal-documento-ingresso').find('#ticket-document-email').val('"+email+"');" + 
                    "$('#modal-documento-ingresso').find('#ticket-document-phone').val('"+phone+"');" + 
                    "$('#modal-documento-ingresso').find('#ticket-document-dob').val('"+dob+"');" + 
                    "$('#modal-documento-ingresso').modal()"
                    );
                    $('#modal-documento-ingresso').modal('hide');
                }else{
                    var modal = new Modal({
                        title: "Ops!!",
                        body : ''+json.message+'
',
                        buttonOK : false,
                    });
                    modal.show();
                }
            }
        });
        
    },
    applyCoupon : function(eveCodigo){
        var cupHash = $('#cupHash').val();
        ajaxLoader.ajaxStart();
        $.ajax({
            url: '/member-shopping-cart/apply-coupon',
            data: {cup_hash: cupHash, eve_codigo: eveCodigo},
            dataType: 'json',
            method: 'post',
            success: function(json){
                ajaxLoader.ajaxStop();
                if(json.status){
                    location.reload();                 
                }else{
                    if(json.show_document_modal){
                        $('#modal').css('display', 'block');
                        $('.modal-backdrop').css('display', 'block');
                        $('#form-half-ticket').trigger("reset");
                    
                        $('#modal').attr("data-pro", json.product_id);
                        $('#modal').attr("data-car", json.cart_id);
                        $('#modal').attr("data-eve", eveCodigo);
                        $('#modal').attr("data-dsc", 'L-' + json.discount_id);
                        $('#modal').attr("data-cup", json.coupon_id);
                        $('#modal').find('.titulo-interna').text('CADASTRO');
                        $('#modal').modal('show');
                    }else{
                        var modal = new Modal({
                          title: json.title,
                          body : ''+json.message+'
',
                          buttonOK : false,
                        });
                        modal.show()
                    }
                }
            }
        });
    }
};
var memberShoppingCart = new memberShoppingCart();